package com.sq.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sq.shortlink.project.dao.entity.ShortLinkDO;
import com.sq.shortlink.project.dao.mapper.ShortLinkMapper;
import com.sq.shortlink.project.dto.req.RecycleBinRecoverReqDTO;
import com.sq.shortlink.project.dto.req.RecycleBinRemoveDTO;
import com.sq.shortlink.project.dto.req.RecycleBinSaveReqDTO;
import com.sq.shortlink.project.dto.req.ShortLinkRecycleBinPageReqDTO;
import com.sq.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.sq.shortlink.project.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.sq.shortlink.project.common.constant.RedisKeyConstant.GOTO_SHORT_LINK_ISNULL_KEY;
import static com.sq.shortlink.project.common.constant.RedisKeyConstant.GOTO_SHORT_LINK_KEY;

/**
 * 回收站管理接口实现层
 * @author quan
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl implements RecycleBinService {

    private final ShortLinkMapper shortLinkMapper;
    private final StringRedisTemplate stringRedisTemplate;
    @Override
    public void saveRecycleBin(RecycleBinSaveReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getEnableStatus, 0)
                .eq(ShortLinkDO::getDelFlag, 0);
        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(1)
                .build();
        shortLinkMapper.update(shortLinkDO,updateWrapper);
        //将缓存删除
        stringRedisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY,requestParam.getFullShortUrl()));
    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageShortLinkRecycleBin(ShortLinkRecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<ShortLinkDO> queryWrapper = Wrappers.lambdaQuery(ShortLinkDO.class)
                .in(ShortLinkDO::getGid, requestParam.getGidList())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus, 1);
        IPage<ShortLinkDO> result = shortLinkMapper.selectPage(requestParam, queryWrapper);
        return result.convert(each ->
                {
                    ShortLinkPageRespDTO linkPageRespDTO = BeanUtil.toBean(each, ShortLinkPageRespDTO.class);
                    linkPageRespDTO.setFullShortUrl("http://" + linkPageRespDTO.getFullShortUrl());
                    return linkPageRespDTO;
                }
        );
    }

    @Override
    public void recycleBinRecover(RecycleBinRecoverReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getFullShortUrl,requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .eq(ShortLinkDO::getDelFlag, 0);
        ShortLinkDO shortLinkDO = ShortLinkDO.builder()
                .enableStatus(0)
                .build();
        shortLinkMapper.update(shortLinkDO,updateWrapper);
        //删除之前缓存的空对象
        stringRedisTemplate.delete(String.format(GOTO_SHORT_LINK_ISNULL_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public void recycleBinRemove(RecycleBinRemoveDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> updateWrapper = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getFullShortUrl,requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .eq(ShortLinkDO::getDelFlag, 0);
        ShortLinkDO shortLinkDO = new ShortLinkDO();
        shortLinkDO.setDelFlag(1);
        shortLinkMapper.update(shortLinkDO,updateWrapper);
    }
}
